//
//  RTVistorView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/3.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit


// 设置协议
@objc protocol RTVisitorViewDelegate: NSObjectProtocol {

   optional func visitorView(visitView: RTVisitorView,clickBtn: UIButton)
}


class RTVisitorView: UIView {
    
    
    // 设置代理
    weak var delegate: RTVisitorViewDelegate?
    

    // 重写初始化方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
        addAnimation()
    }
    
    
  
    
    
    // 设置数据
    func setVisitorViewInfo(imageName: String?,message: String?) {
        
        if imageName == nil {
            
            addAnimation()
            
        }else{
        
            iconView.image = UIImage(named: imageName!)
            messageLabel.text = message;
            circleView.hidden = true
        }
    
    
    }
    
    
    // 添加动画
    private func addAnimation() {
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.toValue = M_PI * 2
        
        ani.duration = 20
        
        ani.repeatCount = MAXFLOAT
        
        ani.removedOnCompletion = false
        
        circleView.layer.addAnimation(ani, forKey: "nil")
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 懒加载iconView
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 懒加载circleView
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    
    // 懒加载遮罩
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 懒加载消息label
    private lazy var messageLabel: UILabel = {
        
        let message =  UILabel(textColor: UIColor.darkGrayColor(), textFont: 14)
        
        message.textAlignment = .Center
        
        message.numberOfLines = 0
        
        message.text = "关注一些人，回这里看看有什么惊喜"
        
        return message
    }()
    
    // 懒加载注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton(title: "注册", textColor: UIColor.orangeColor(), textFont: 14)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        // 添加点击事件
        btn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    // 懒加载登录按钮
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton(title: "登录", textColor: UIColor.darkGrayColor(), textFont: 14)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "loginBtnDidClick:", forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    
    // 注册按钮点击事件
    @objc private func registerBtnDidClick() {
    
        print("注册了")
    }
    
    
    // 登录按钮点击事件
    @objc private func loginBtnDidClick(sender: UIButton) {
        
        delegate?.visitorView?(self, clickBtn: sender)
        
//        print("登录")
    }

}


extension RTVisitorView {
    
    
    // 初始化控件
    private func setupUI() {
        
        
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        setLayout()
    }
    
    

    // 布局子控件
    private func setLayout() {
        
        // 房子
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        
        // circle
        circleView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        // 遮罩
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -10))
        
        // message
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        // 登录按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        
        // 注册按钮
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: .Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
    }
    
    
 


}
